/**
 * Created by Mark_ren on 2016/10/25.
 * 生活本不应如此坎坷
 */
var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();

//添加收藏
router.get('/addCollection', function(req, res, next) {
    co(function () {
        req.createTime = decodeURI(req.query.createTime);
        seq.collectionModule.create(req.query).then(function(results){
            res.json({"success":true,"rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});
//取消收藏
router.get('/deleteCollection', function(req, res, next) {
    co(function () {
        var memberId= req.query.memberId;
        var courseId= req.query.courseId;
       // console.log(memberId,courseId);
        seq.collectionModule.destroy({'where':{'memberId':memberId,'courseId':courseId}}).then(function(results){
            res.json({"success":true,"msg":"取消成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'取消失败'});
        console.log(e);
    })
});
//查询收藏列表
router.get('/getCollectionList', function(req, res, next) {
    co(function () {
        seq.sequelize.query("select c.id, c.course_id,c.member_id,c.create_time,c.type ,d.title ,d.picture_url,d.url" +
            "  from qcc_collection as c ,(select id,title,picture_url ,url from qcc_news" +
            "  UNION ALL select id ,title,picture_url,url from qcc_classroom UNION ALL" +
            "  select id,title,picture_url,url from qcc_discuss) as d where c.course_id=d.id and c.member_id ='"+req.query.userId+"'  order by c.create_time desc").spread(function(result){
            res.json({"success":true,"result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});


module.exports = router;